package org.skyemoon.hbase.hbasetest.toolkit;

import com.opencsv.CSVReader;
import com.opencsv.exceptions.CsvException;
import org.skyemoon.hbase.hbasetest.dao.entity.ElectronicDO;
import org.skyemoon.hbase.hbasetest.dao.entity.RadioDO;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import static org.skyemoon.hbase.hbasetest.toolkit.Date2String.convertString2Date;

public class ReadCSV {

    public static List<RadioDO> readRadioCSV (String filePath) throws IOException, CsvException, ParseException {
        List<RadioDO> result = new ArrayList<>();
        CSVReader reader = new CSVReader(new FileReader(filePath));
        List<String[]> allData = reader.readAll();
        for (String[] row : allData) {
            RadioDO radioDO = RadioDO.builder()
                    .sampleTime(convertString2Date(row[0]))
                    .objectCount(Integer.parseInt(row[1]))
                    .objectNum(Integer.parseInt(row[2]))
                    .objectFlag(Integer.parseInt(row[3]))
                    .longitude(Double.parseDouble(row[4]))
                    .latitude(Double.parseDouble(row[5]))
                    .height(Double.parseDouble(row[6]))
                    .rcs(Double.parseDouble(row[7]))
                    .radialDistance(Double.parseDouble(row[8]))
                    .heading(Double.parseDouble(row[9]))
                    .build();
            result.add(radioDO);
        }
        return result;
    }

    public static List<ElectronicDO> readElectronicCSV (String filePath) throws IOException, CsvException, ParseException {
        List<ElectronicDO> result = new ArrayList<>();
        CSVReader reader = new CSVReader(new FileReader(filePath));
        List<String[]> allData = reader.readAll();
        for (String[] row : allData) {
            ElectronicDO electronicDO = ElectronicDO.builder()
                    .sampleTime(convertString2Date(row[0]))
                    .objectCount(Integer.parseInt(row[1]))
                    .objectType(Integer.parseInt(row[2]))
                    .radioId(Integer.parseInt(row[3]))
                    .distance(Double.parseDouble(row[4]))
                    .position(Double.parseDouble(row[5]))
                    .longitude(Double.parseDouble(row[6]))
                    .latitude(Double.parseDouble(row[7]))
                    .frequency(Double.parseDouble(row[8]))
                    .pri(Double.parseDouble(row[9]))
                    .pulseWidth(Double.parseDouble(row[10]))
                    .build();
            result.add(electronicDO);
        }
        return result;
    }
}
